// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// mint组件库
import 'mint-ui/lib/style.css'
import Mint from 'mint-ui';
Vue.use(Mint);

// 导入jq
// import $ from 'jquery'


Vue.config.productionTip = false
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import App from './App'
import router from './router/index.js'
// 引入axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

// 引入vueresource
// import VueResource from 'vue-resource'
// Vue.use(VueResource)

import "../src/assets/css/iconfont.css"
// 公共样式
import "../src/css/public.css"
import "../src/css/my-mint.css"
import '../src/lib/mui/css/mui.css'


// 点击复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import {
  Swipe,
  SwipeItem,
  Header,
  Lazyload,
  Loadmore,
  Badge,
  Switch
} from "mint-ui";

Vue.component(Switch.name, Switch);
Vue.component(Badge.name, Badge);
Vue.component(Loadmore.name, Loadmore);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Header.name, Header);
Vue.use(Lazyload);



var store = new Vuex.Store({
  state: { //this.$store.state.xxxx
    car: JSON.parse(localStorage.getItem('car') || '[]'),
  },
  mutations: { //this.$store.commit('方法名称'，‘唯一的参数、、多和参数可以传进对像或者数组)
    addTopCar: function (state, goodsInfo) {

      var falg = false

      state.car.some(item => {
        // 相同产品
        if (item.goods_name == goodsInfo.goods_name) {
          item.goods_count += parseInt(goodsInfo.goods_count)
          console.log(state.car)
          falg = true
          return true
        }
      })
      if (falg == false) {
        state.car.push(goodsInfo)
      }

      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updated:function(state, goodsInfo) {
       state.car.some(item=>{
         if(item.good_name = goodsInfo.id){
          item.goods_count = goodsInfo.count
          localStorage.setItem('car',JSON.stringify(state.car))
          return true
         }
       })
    },
  },
  getters:{ //this.$store.getters.xxxxx相当于过滤器或者计算属性
    getAllCount(state){
      
      var c = 0;
      state.car.forEach(item =>{
        c += item.goods_count
      })
      return state.car.length
    },
    getcount(state){
      var countlist = {}
      
      state.car.forEach(item =>{
        countlist[item.goods_name] = item.goods_count
      })
      return countlist

    }

  }

})

new Vue({
  el: '#app',
  router: router,
  render(createdElements) {
    return createdElements(App)
  },
  store: store
})
